package ee.example.final_work

import android.annotation.SuppressLint
import android.content.ContentValues
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper

//数据库帮助类
class SQLiteHelper(context: Context?) :
    SQLiteOpenHelper(context, DBUtils.DATABASE_NAME, null, DBUtils.DATABASE_VERION) {
    private val sqLiteDatabase: SQLiteDatabase

    //创建数据库
    init {
        //上下文   数据库名称   null  数据库版本号
        //获取编辑数据库对象 sqLiteDatabase
        sqLiteDatabase = this.writableDatabase
    }

    //创建表
    override fun onCreate(db: SQLiteDatabase) {
        db.execSQL(
            "create table " + DBUtils.DATABASE_TABLE + "(" + DBUtils.NOTEPAD_ID +
                    " integer primary key autoincrement," + DBUtils.NOTEPAD_CONTENT +
                    " text," + DBUtils.NOTEPAD_TIME + " text)"
        )
    }

    //数据库更新
    override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {}

    //添加数据
    fun insertData(userContent: String?, userTime: String?): Boolean {
        //统一使用  ContentValues  保存数据
        val contentValues = ContentValues()
        //key - value形式键值对存储
        contentValues.put(DBUtils.NOTEPAD_CONTENT, userContent)
        contentValues.put(DBUtils.NOTEPAD_TIME, userTime)
        return sqLiteDatabase.insert(DBUtils.DATABASE_TABLE, null, contentValues) > 0
    }

    //删除数据
    fun deleteData(id: String): Boolean {
        val sql = DBUtils.NOTEPAD_ID + "=?" //条件
        val contentValuesArray = arrayOf(id) //条件值
        return sqLiteDatabase.delete(DBUtils.DATABASE_TABLE, sql, contentValuesArray) > 0
    }

    //修改数据
    fun updateData(id: String, content: String?, userYear: String?): Boolean {
        val contentValues = ContentValues()
        contentValues.put(DBUtils.NOTEPAD_CONTENT, content)
        contentValues.put(DBUtils.NOTEPAD_TIME, userYear)
        val sql = DBUtils.NOTEPAD_ID + "=?" //条件
        val strings = arrayOf(id) //条件值
        return sqLiteDatabase.update(DBUtils.DATABASE_TABLE, contentValues, sql, strings) > 0
    }

    //查询数据
    fun query(): ArrayList<NewsBean> {
        val list = ArrayList<NewsBean>()
        val cursor = sqLiteDatabase.query(
            DBUtils.DATABASE_TABLE, null, null, null,
            null, null, DBUtils.NOTEPAD_ID + " desc"
        )
        if (cursor != null) {
            while (cursor.moveToNext()) { //移动到首行 循环遍历
                //创建空实体类对象
                val noteInfo = NewsBean()
                //获取表字段每行对应的值
                @SuppressLint("Range") val id =
                    cursor.getInt(cursor.getColumnIndex(DBUtils.NOTEPAD_ID)).toString()
                @SuppressLint("Range") val content =
                    cursor.getString(cursor.getColumnIndex(DBUtils.NOTEPAD_CONTENT))
                @SuppressLint("Range") val time =
                    cursor.getString(cursor.getColumnIndex(DBUtils.NOTEPAD_TIME))
                //赋值
                noteInfo.id = id
                noteInfo.content = content
                noteInfo.setTime(time)
                //添加数组
                list.add(noteInfo)
            }
            cursor.close() //关闭游标 防止内存泄漏
        }
        return list
    }
}
